import React, { Component } from 'react'
import { Text, StyleSheet, View } from 'react-native'
import { createStackNavigator } from '@react-navigation/stack';
import { connect } from 'react-redux'
import storage from './utils/storage'
import { login } from './redux/actions/user'

import SplashScreen from './screens/NoAuth/Splash'
import SignInScreen from './screens/NoAuth/SignIn'
import RegisterScreen from './screens/NoAuth/Register'
import MainTabScreen from './routes'

const mapStateToProps = state => ({
  isLoading: state.User.isLoading,
  isLogin: state.User.isLogin
})

const Stack = createStackNavigator();

class Main extends Component {
  constructor(props) {
    super(props)
    this.state = {
      defaultRouteName: this.props.isLogin ? 'MainTab' : 'SignIn'
    }
  }

  async componentDidMount() {
    // 获取缓存中的用户信息
    let userInfo = storage.get('userInfo')
    if (userInfo) {
      // 缓存中有用户信息，则直接登陆
      this.props.login(userInfo)
    } else {
      // 否则跳转到登陆页面
    }
  }
  

  render() {
    return (
      <>
      {
        this.props.isLogin
        ?
        (<MainTabScreen />)
        :
        (<Stack.Navigator
          headerMode="none"
          initialRouteName={'Splash'} 
        >
          <Stack.Screen name="Splash" component={SplashScreen} />
          <Stack.Screen name="SignIn" component={SignInScreen} />
          <Stack.Screen name="Register" component={RegisterScreen} />
        </Stack.Navigator>)
      }
      </>
    )
  }
}

export default connect(mapStateToProps, { login })(Main)
